package com.yiqi.zhiyuan.feature.certification.model;

import com.suozhang.framework.component.http.RxDataProcessFactory;
import com.yiqi.zhiyuan.entity.bo.UploadCertificatesInfo;
import com.yiqi.zhiyuan.entity.dto.DiverCertificationDTO;
import com.yiqi.zhiyuan.feature.certification.contract.UploadContract;
import com.yiqi.zhiyuan.feature.certification.dependencies.upload.DaggerUploadModelComponent;
import com.yiqi.zhiyuan.framework.api.ApiLib;
import com.yiqi.zhiyuan.framework.api.UserApi;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

import io.reactivex.Observable;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;

/**
 * @ProjectName: ZhiYuan
 * @Description: 注册model
 * @Author: lijuwen
 * @CreateDate: 2019/3/13 13:07
 * @UpdateUser: 更新者
 * @UpdateDate: 2019/3/13 13:07
 * @UpdateRemark: 更新说明
 * @Version: 1.0
 */
public class UploadModel implements UploadContract.Model {
    @Inject
    UserApi api;

    public UploadModel() {
        DaggerUploadModelComponent.builder().apiComponent(ApiLib.apiComponent()).build().inject(this);
    }


    @Override
    public Observable<String> doUploadCertificate(List<UploadCertificatesInfo> files, DiverCertificationDTO certificationDTO) {

        List<MultipartBody.Part> bodyHashMap = new ArrayList<>();

        for (int i = 0; i < files.size(); i++) {
            if (!files.get(i).getImgUrl().contains("http")) {
                File file = new File(files.get(i).getImgUrl());
                RequestBody requestFile = RequestBody.create(MediaType.parse("application/otcet-stream"), file);
                MultipartBody.Part filePart = MultipartBody.Part.createFormData(files.get(i).getName(), file.getName(), requestFile);
                bodyHashMap.add(filePart);
            }
        }

        RequestBody carColorBody = RequestBody.create(MediaType.parse("multipart/form-data"), certificationDTO.getCarColor());
        RequestBody carModelNoBody = RequestBody.create(MediaType.parse("multipart/form-data"), certificationDTO.getCarModelNo());
        RequestBody carNameBody = RequestBody.create(MediaType.parse("multipart/form-data"), certificationDTO.getCarName());
        RequestBody carNoBody = RequestBody.create(MediaType.parse("multipart/form-data"), certificationDTO.getCarNo());
        RequestBody driverLicenseBody = RequestBody.create(MediaType.parse("multipart/form-data"), certificationDTO.getDriverLicense());
        RequestBody bankCardBody = RequestBody.create(MediaType.parse("multipart/form-data"), certificationDTO.getBankCard());
        RequestBody idCardBody = RequestBody.create(MediaType.parse("multipart/form-data"), certificationDTO.getIdCard());
        RequestBody realNameBody = RequestBody.create(MediaType.parse("multipart/form-data"), certificationDTO.getRealName());
        RequestBody sexBody = RequestBody.create(MediaType.parse("multipart/form-data"), certificationDTO.getSex());


        return api.uploadImage(bodyHashMap, carColorBody, carModelNoBody, carNameBody, carNoBody, driverLicenseBody, bankCardBody, idCardBody, realNameBody, sexBody)
                .compose(RxDataProcessFactory.dataPrepAndIoToMainTransformer());
    }
}

